package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.Func1;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.common.core.domain.model.TimeRange;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.XlyProject;
import com.ruoyi.system.domain.query.XlyProjectQuery;
import com.ruoyi.system.mapper.XlyProjectMapper;
import com.ruoyi.system.service.IXlyProjectGroupService;
import com.ruoyi.system.service.IXlyProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-02
 */
@Service
public class XlyProjectServiceImpl extends MPJBaseServiceImpl<XlyProjectMapper, XlyProject> implements IXlyProjectService {

    /**
     * 查询项目列表
     *
     * @param xlyProject 项目
     * @return 项目
     */
    @Override
    public List<XlyProject> selectXlyProjectList(XlyProjectQuery xlyProject) {
        List<XlyProject> xlyProjectList = getBaseMapper().selectList(buildQueryWrapper(xlyProject));
        return xlyProjectList;
    }

    private LambdaQueryWrapper<XlyProject> buildQueryWrapper(XlyProjectQuery query) {
        LambdaQueryWrapper<XlyProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getProjectName()), XlyProject::getProjectName, query.getProjectName());
        lqw.eq(StringUtils.isNotBlank(query.getProjectContent()), XlyProject::getProjectContent, query.getProjectContent());
        if (query.getCreateTime() != null){
            lqw.between(XlyProject::getCreateTime, query.getCreateTime().getBeginTimeOfDay(), query.getCreateTime().getEndTimeOfDay());
        }
        if (query.getStartTime() != null){
            lqw.between(XlyProject::getStartTime, query.getStartTime().getBeginTimeOfDay(), query.getStartTime().getEndTimeOfDay());
        }
        // 模糊查询
        if (StringUtils.isNotBlank(query.getSearchValue())){
            lqw.and(l -> {
                l.or().like(XlyProject::getProjectName, query.getSearchValue());
                l.or().like(XlyProject::getProjectContent, query.getSearchValue());
            });
        }
        lqw.orderByDesc(XlyProject::getCreateTime);
        return lqw;
    }

}
